#include <QtCore>

 void parseHtmlFile(QTextStream &out, const QString &fileName) {
     QFile file(fileName);

     out << "Analysis of HTML file: " << fileName << endl;

     if (!file.open(QIODevice::ReadOnly)) {
         out << "  Couldn't open the file." << endl << endl << endl;
         return;
     }

     QXmlStreamReader reader(&file);

     int paragraphCount = 0;
     QStringList links;
     QString title;

     while (!reader.atEnd())
     {
        qDebug() << reader.name();
         reader.readNext();

         if (reader.error())
                  {
                      qDebug() << QString(
                                     "Error: %1 in file at line %3, column %4.\n").arg(
                                         reader.errorString(),
                                         QString::number(reader.lineNumber()),
                                         QString::number(reader.columnNumber()));

                  }
         if (reader.isStartElement())
         {

                 if (reader.name() == "title")
                 title = reader.readElementText();
             else if(reader.name() == "a")
                 links.append(reader.attributes().value("href").toString());
             else if(reader.name() == "p")
                 ++paragraphCount;
         }
     }

     if (reader.hasError()) {
         out << "  The HTML file isn't well-formed: " << reader.errorString()
             << endl << endl << endl;
         return;
     }

     out << "  Title: \"" << title << "\"" << endl
         << "  Number of paragraphs: " << paragraphCount << endl
         << "  Number of links: " << links.size() << endl
         << "  Showing first few links:" << endl;

     while(links.size() > 5)
         links.removeLast();

     foreach(QString link, links)
         out << "    " << link << endl;
     out << endl << endl;
 }

 int main(int argc, char **argv)
 {
     // initialize QtCore application
     QCoreApplication app(argc, argv);

     // get a list of all html files in the current directory
     QStringList filter;
     filter << "*.htm";
     filter << "*.html";
     QStringList htmlFiles = QDir::current().entryList(filter, QDir::Files);

     QTextStream out(stdout);

     if (htmlFiles.isEmpty()) {
         out << "No html files available.";
         return 1;
     }

     // parse each html file and write the result to file/stream
     foreach(QString file, htmlFiles)
         parseHtmlFile(out, file);

     return 0;
 }
